<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Vtour extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$islogin = self::checkLogin();
		if(!$islogin){
			redirect(site_url('login/index'));
		}
	
		$this->load->model('Vtour_model');
	}
	private function checkLogin(){
		if(empty($_SESSION['user']['userid'])) return false;
		return $_SESSION['user']['userid'];
	}

	public function index(){
		$data['title'] = 'VR列表';
		$this->load->model('Vtour_model');
		
		$res = $this->Vtour_model->getList($where=array());
		$data['list'] = $res['list'];
		
		$this->load->view('vtour/index',$data);
	}
	
	public function add(){
		if($this->input->is_ajax_request()){
			$param = $this->input->post();
			$param['create_time'] = date('Y-m-d H:i:s',time());
			$res = $this->Banner_model->insertBanner($param);
			exit(json_encode(array('ack'=>$res['ack'],'msg' => $res['msg'])));
		}else{
			$data['title'] = '添加VR';
			
			$this->load->view('vtour/add',$data);
		}
		
	}

	public function create(){
		$param = $this->input->post();
		$param['create_time'] = date('Y-m-d H:i:s',time());
		$res = $this->Vtour_model->insertVtour($param);
		exit(json_encode(array('ack'=>$res['ack'],'msg' => $res['msg'])));
	}

	//生成全景图
	public function make(){
		$post = $this->input->post();
		$this->load->model('Vtour_model');
		$res = $this->Vtour_model->add($post);

		exit(json_encode(array('ack' => true, 'data' => $res)));
	}
	
	//编辑全景图
	public function save(){
		$post = $this->input->post();
		$res = $this->Vtour_model->update($post);

		exit(json_encode(array('ack' => true, 'data' => $res)));
	}


	public function bannerEdit(){
		if($this->input->is_ajax_request()){
			$param = $this->input->post();
			$banner_id = $param['banner_id'];
			unset($param['banner_id']);
			
			$param['update_time'] = date('Y-m-d H:i:s',time());
			
			$res = $this->Banner_model->updateBanner($param,array('id' => $banner_id));
			exit(json_encode(array('ack'=>$res['ack'],'msg' => $res['msg'])));
		}else{
			$data['title'] = '添加VR';
			$banner_id = $this->input->get('id');
			$info = $this->Banner_model->getOne($banner_id);
			$data['info'] = $info;
			// 		print_r($data);exit;
			$this->load->view('banner/banneredit',$data);
		}
	
	}
	
	
	//图片上传
	public function do_upload()
	{	
		$data_path = PUBLIC_PATH . DS ."data";
        $timestamp = $this->input->post("timestamp") ? $this->input->post("timestamp") : time();

        $save_path = $data_path . DS . $timestamp;
        if ( !is_dir($save_path) ) {
            mkdir ($save_path , 0777, true);
        }
// 		var_dump($_FILES);
		$config['upload_path']      = $save_path;
		$config['allowed_types']    = 'gif|jpg|png';
// 		$config['max_size']     = 100;
// 		$config['max_width']        = 1024;
// 		$config['max_height']       = 768;
	
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload('imgUpload'))
		{
			$error = array('error' => $this->upload->display_errors());
	
			var_dump($error);
			echo 11111;
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());
			
			exit(json_encode(array('ack' => true, 'data' => $data)));
		}
	}

}